//
//  CHTextField.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/8.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHTextField.h"
@interface CHTextField()

@property (nonatomic ,strong) UIImageView *imgView;

@end
@implementation CHTextField


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (self.h- 20)/2, 20, 20)];
        imageView.image = [UIImage imageNamed:@"search_img"];
        
        [self addSubview:imageView];
        self.imgView = imageView;
        
        
        
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) +5, 0, self.w - CGRectGetMaxX(imageView.frame) - 5,self.h )];
        textField.textColor = [UIColor colorWithHexString:@"333333" alpha:0.5f];
        textField.font = [UIFont systemFontOfSize:14];
        textField.placeholder = @"搜索";
        [self addSubview:textField];
        self.textField = textField;
        
        // 监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:nil];
        
        
    }
    
    return self;
}

-(void)textDidChange{

    self.text = self.textField.text;
    if ([self.delegate respondsToSelector:@selector(CHTextFieldDidValueChange)]) {
        [self.delegate CHTextFieldDidValueChange];
    } 
}

-(void)setSearchImgName:(NSString *)searchImgName{
    _searchImgName = searchImgName;
    
    self.imgView.image  = [UIImage imageNamed:searchImgName];
    
}

-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.textField.placeholder = placeholder;
    
}





@end
